//
//  LPEBranchListView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/8/17.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPEBranchListView.h"
#import "BranchPartyOrgizationModel.h"
#import "BranchPartyOrganizTableViewCell.h"
#import "BranchOpenPartyOrganizationViewController.h"
@interface LPEBranchListView ()<UITableViewDelegate,UITableViewDataSource>
/**  菜单  */
@property (strong, nonatomic) UIButton  *meunButton;
/**  tableView  */
@property (strong, nonatomic) UITableView  *tableView;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/**  数据数组  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

@end
@implementation LPEBranchListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        __weak typeof(self) weakSelf = self;
        [self.refurbish headerToScollview:self.tableView Block:^{
            weakSelf.page = 1;
            [weakSelf netWorking_header];
            
        }];
        [self.refurbish footerToScollview:self.tableView Block:^{
            weakSelf.page++;
            [weakSelf netWorking_footer];
        }];
        
        [self.tableView.mj_header beginRefreshing];
        [self layoutUI];

    }
    return self;
}



#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    
    NSDictionary *paramer = @{@"PAGE_NUMBER":page,
                           @"SHOW_NUMBER":@"10"};

    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionLoginInfolistDepts] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ORG_DEPTS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[BranchPartyOrgizationModel class] json:array] mutableCopy] ;
            
            if (weakSelf.dataSouce.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                
            }
            
        }
        
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
       
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf.tableView.mj_header beginRefreshing];
            }];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{@"PAGE_NUMBER":page,
                              @"SHOW_NUMBER":@"10"};
    
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionLoginInfolistDepts] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ORG_DEPTS"];
            
            
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[BranchPartyOrgizationModel class] json:array]] ;
            [weakSelf.tableView reloadData];
            
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}

#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    [self addSubview:self.meunButton];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 10 * [UIScreen zj_screenHScale])];
    header.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = header;
}


#pragma mark - 私有方法
- (void)action_meunButton{
    if (self.delegate && [self.delegate respondsToSelector:@selector(LPEBranchListView_click_hide:)]) {
        [self.delegate LPEBranchListView_click_hide:YES];
    }

}

#pragma mark - 监听事件



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    BranchPartyOrganizTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[BranchPartyOrganizTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BranchOpenPartyOrganizationViewController *vc = [BranchOpenPartyOrganizationViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [[self topViewController].navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}





- (UIButton *)meunButton{
    if (_meunButton == nil){
        _meunButton = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 65 * [UIScreen zj_screenWScale], self.height - 95 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale])];
        _meunButton.backgroundColor = [UIColor whiteColor];
        _meunButton.layer.cornerRadius = _meunButton.width / 2;
        _meunButton.layer.borderWidth = 1;
        _meunButton.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_meunButton setImage:[UIImage imageNamed:@"float_map"] forState:UIControlStateNormal];
        [_meunButton addTarget:self action:@selector(action_meunButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _meunButton;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
- (NSMutableArray *)dataSouce{
    if (_dataSouce == nil) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;

}
@end
